<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" draggable>
  <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" :label-width="100">
  <el-form-item label="车辆编码" prop="carCode">
      <el-input v-model="dataForm.carCode" placeholder="车辆编码"></el-input>
  </el-form-item>
  <el-form-item label="所属景区" prop="scenicId">
      <el-input v-model="dataForm.scenicId" placeholder="所属景区"></el-input>
  </el-form-item>
  <el-form-item label="申请用车人" prop="applicant">
      <el-input v-model="dataForm.applicant" placeholder="申请用车人"></el-input>
  </el-form-item>
  <el-form-item label="审批人" prop="reviewr">
      <el-input v-model="dataForm.reviewr" placeholder="审批人"></el-input>
  </el-form-item>
  <el-form-item label="开始用车时间" prop="startUseTime">
      <el-input v-model="dataForm.startUseTime" placeholder="开始用车时间"></el-input>
  </el-form-item>
  <el-form-item label="结算用车时间" prop="endUseTime">
      <el-input v-model="dataForm.endUseTime" placeholder="结算用车时间"></el-input>
  </el-form-item>
  <el-form-item label="申请人手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="申请人手机号"></el-input>
  </el-form-item>
  </el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useIotLogApi, useIotLogSubmitApi } from '@/api/order/operaLog'

const emit = defineEmits(['refreshDataList'])

const visible = defineModel<boolean>('visible')
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	carCode: '',
	scenicId: '',
	applicant: '',
	reviewr: '',
	startUseTime: '',
	endUseTime: '',
	deleted: '',
	phone: ''})

const init = (id?: number) => {
	if (id) {
		getLog(id)
	}
}

const getLog = (id: number) => {
	useIotLogApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
	carCode: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	scenicId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	applicant: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	reviewr: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	startUseTime: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	endUseTime: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	phone: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate(async (valid: boolean) => {
		if (!valid) {
			return false
		}


		useIotLogSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
